<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_animatorCar"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.title_animatorCar"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_queryCar" onclick="queryBySQL()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_startAnimator" onclick="startAnimator()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_pauseAnimator" onclick="pauseAnimator()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_stopAnimator" onclick="stopAnimator()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var map, layer, vectorLayer, features, cars, animatorVector,
        //定义公交线路的样式。
        styleLine = {
            strokeColor: "black",
            strokeWidth: 1,
            fill: false
        },
        styleCar1 =
            {
                externalGraphic: "./images/blueCar.png",
                allowRotate: true,
                graphicWidth: 32,
                graphicHeight: 32
            },
        styleCar2 =
            {
                externalGraphic: "./images/redCar.png",
                allowRotate: true,
                graphicWidth: 32,
                graphicHeight: 32
            },
        host = window.isLocal ? window.server : "https://iserver.supermap.io",
        url1 = host + "/iserver/services/map-changchun/rest/maps/长春市区图";

    init();

    function init() {
        /*
         * 不支持canvas的浏览器不能运行该范例
         * android 设备也不能运行该范例*/
        var broz = SuperMap.Util.getBrowser();

        if (!document.createElement('canvas').getContext) {
            widgets.alert.showAlert(resources.msg_supportCanvas, false);
            return;
        } else if (broz.device === 'android') {
            widgets.alert.showAlert(resources.msg_supportEquipment, false);
            return;
        }

        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })], units: "m"
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("changchun", url1, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        //初始化公交车路线图层。
        vectorLayer = new SuperMap.Layer.Vector("Vector Layer", {
            styleMap: new SuperMap.StyleMap({
                "default": styleLine
            })
        });
        //初始化汽车图层。
        animatorVector = new SuperMap.Layer.AnimatorVector("Cars", {}, {
            //设置速度为每帧播放0.05小时的数据
            speed: 0.05,
            //开始时间为0晨
            startTime: 0,
            //结束时间设置为最后运行结束的汽车结束时间
            endTime: 55
        });
    }


    function addLayer() {
        map.addLayers([layer, vectorLayer, animatorVector]);
        map.setCenter(new SuperMap.LonLat(5935, -3580), 1);
        map.addControl(new SuperMap.Control.MousePosition());
    }

    //定义查询汽车线路。
    function queryBySQL() {

        animatorVector.removeAllFeatures();
        var queryParam, queryBySQLParams, queryBySQLService;
        queryParam = new SuperMap.REST.FilterParameter({
            name: "BusLine@Changchun#1",
            attributeFilter: "SmID > 0"
        }),
            queryBySQLParams = new SuperMap.REST.QueryBySQLParameters({
                queryParams: [queryParam]
            }),
            queryBySQLService = new SuperMap.REST.QueryBySQLService(url1, {
                eventListeners: {"processCompleted": processCompleted, "processFailed": processFailed}
            });
        queryBySQLService.processAsync(queryBySQLParams);
    }

    function processCompleted(queryEventArgs) {
        var i, j, feature,
            result = queryEventArgs.result;
        features = [];
        cars = [];
        vectorLayer.removeAllFeatures();
        animatorVector.removeAllFeatures();
        var orientation = 1;
        if (result && result.recordsets) {
            for (i = 0; i < result.recordsets.length; i++) {
                if (result.recordsets[i].features) {
                    for (j = 0, len = result.recordsets[i].features.length; j < len; j++) {
                        feature = result.recordsets[i].features[j];
                        feature.style = null;
                        features.push(feature);
                        var style = j % 2 == 0 ? styleCar1 : styleCar2;
                        var featureComps = feature.geometry.components;
                        for (var k = 0, len = featureComps.length; k < len; k++) {
                            var car = new SuperMap.Feature.Vector(featureComps[k].clone(),
                                {
                                    FEATUREID: feature.id,
                                    //根据节点生成时间
                                    TIME: k
                                }, style
                            );
                            cars.push(car);
                        }
                    }
                }
            }
        }
        vectorLayer.addFeatures(features);
        animatorVector.addFeatures(cars);
    }

    function processFailed(e) {
        widgets.alert.showAlert(e.error.errorMsg, false);
    }

    //开始播放动画
    function startAnimator() {
        animatorVector.animator.start();
    }

    //暂停播放动画
    function pauseAnimator() {
        animatorVector.animator.pause();
    }

    //停止播放动画
    function stopAnimator() {
        animatorVector.animator.stop();
    }


</script>

</body>
</html>